package com.zyy.smartcity1129.hd;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.hdlist;
import com.zyy.smartcity1129.data.hdtype;
import com.zyy.smartcity1129.data.homebanner;
import com.zyy.smartcity1129.util.BannerAd;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;
import com.zyy.smartcity1129.util.XCUtli;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class HDFragment extends BaseFragment {

    List<homebanner.RowsBean> list1;
    private SearchView search;
    private Banner banner;
    List<hdtype.DataBean> typelist;
    List<hdlist.RowsBean> hdlist;
    private TabLayout tab;
    private RecyclerView rv;

    public static hdlist.RowsBean hdList;

    @Override
    protected int layId() {
        return R.layout.f_hd;
    }

    @Override
    protected String title() {
        return "活动管理";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        XCUtli xcUtli = new XCUtli(4);
        xcUtli.then(() -> {
            list1 = Http.getInstance().get("/prod-api/api/activity/rotation/list", null, homebanner.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            hdlist = Http.getInstance().get("/prod-api/api/activity/activity/list", null, hdlist.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            typelist = Http.getInstance().get("/prod-api/api/activity/category/list", null, hdtype.class, 1).getData();
            xcUtli.addint();
        });

        xcUtli.XCliveData.observe(getViewLifecycleOwner(), a -> {
            if (xcUtli.integer.get() == 3) {
                List<Object> list = new ArrayList<>();
                for (homebanner.RowsBean b : list1) {
                    list.add(b.getAdvImg());
                }
                banner.setAdapter(new BannerAd(list)).addBannerLifecycleObserver(this).setIndicator(new CircleIndicator(getContext())).start();
                tab.removeAllTabs();
                for (hdtype.DataBean t : typelist) {
                    tab.addTab(tab.newTab().setText(t.getName()));
                }
                List<hdlist.RowsBean> list3 = new ArrayList<>();
                list3.addAll(hdlist.stream().filter(b -> b.getCategoryId() == typelist.get(0).getId()).collect(Collectors.toList()));
                RvAd<hdlist.RowsBean> ad = new RvAd<hdlist.RowsBean>(list3, R.layout.rv_base) {
                    private void initView(View iv) {
                        ig = (ImageView) iv.findViewById(R.id.ig);
                        tv1 = (TextView) iv.findViewById(R.id.tv1);
                        tv2 = (TextView) iv.findViewById(R.id.tv2);
                        tv3 = (TextView) iv.findViewById(R.id.tv3);
                        tv4 = (TextView) iv.findViewById(R.id.tv4);
                        tv5 = (TextView) iv.findViewById(R.id.tv5);
                    }

                    private TextView tv5;
                    private TextView tv4;
                    private TextView tv3;
                    private TextView tv2;
                    private TextView tv1;
                    private ImageView ig;

                    @Override
                    protected void initR(View vi, hdlist.RowsBean rowsBean, int p) {
                        initView(vi);
                        tv1.setText(rowsBean.getName());
                        Glide.with(ig.getContext()).load(Http.base + rowsBean.getImgUrl()).into(ig);
                        tv2.setText("发布时间："+rowsBean.getPublishTime());
                        tv3.setText("观看人数："+rowsBean.getLikeNum());
                        tv4.setText("报名人数："+rowsBean.getSignupNum());
                        tv5.setText("活动："+rowsBean.getCategoryName());
                        vi.setOnClickListener(v1->{
                            hdList=rowsBean;
                            add(new Hd1Fragment());
                        });
                    }
                };
                tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        list3.clear();
                        list3.addAll(hdlist.stream().filter(b -> b.getCategoryId() == typelist.stream().filter(c->c.getName().equals(tab.getText())).findFirst().get().getId()).collect(Collectors.toList()));
                        ad.notifyDataSetChanged();
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });
                search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        list3.clear();
                        list3.addAll(hdlist.stream().filter(c->c.getName().contains(query)).collect(Collectors.toList()));
                        ad.notifyDataSetChanged();
                        return true;
                    }

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        return false;
                    }
                });
                rv.setAdapter(ad);
                rv.setLayoutManager(new LinearLayoutManager(getContext()));

            }
        });
    }

    public void initView(View iv) {
        search = iv.findViewById(R.id.search);
        banner = iv.findViewById(R.id.banner);
        tab = (TabLayout) iv.findViewById(R.id.tab);
        rv = (RecyclerView) iv.findViewById(R.id.rvMyOrder);
    }
}
